<?php

define('APP_DIR', dirname(__DIR__));
define('COMMON_DIR', dirname(APP_DIR) . '/Common');

require '../../../Home/Main.php';
$main = Main::instance()->instance();
$main->setRoute(array(
	'/^register\.html$/i' => 'Passport/Register',
	'/^login\.html$/i' => 'Passport/Login',
	'/^home\.html$/i' => 'User/MyHome',
	'/^add\.html$/i' => 'User/AddDog',
	'/^setting\.html$/i' => 'User/Setting',
	'/^home\/morephoto.html$/i' => 'User/MorePhoto',
	'/^home\/moresingle.html$/i' => 'User/MoreSingle',
	'/^home\/message.html$/i' => 'User/Message',
	'/^home\/message_(\d+).html$/i' => 'User/MessageDetail',
	'/^home\/dynamic.html$/i' => 'User/Dynamic',
	'/^home\/follow.html$/i' => 'User/Follow',
	'/^home\/fans.html$/i' => 'User/Fans',

	'/^dog_u([\d]+)\.html$/i' => 'Dog/Detail',
	'/^dog\/morephoto_([\d]+)\.html$/i' => 'Dog/Photo',
	'/^dog\/photo\/detail_(\d+)\.html$/i' => 'Dog/PhotoDetail',
	'/^dog\/single_u(\d+)\.html$/i' => 'Dog/Single',

	'/^blind\.html$/i' => 'Blind/Index',
	'/^search_(\d*?)_(.*?)_(\d?)_(\d+)\.html$/i' => 'Blind/Search',

	'/^single\.html/i' => 'Single/Single',
	'/^single\/category_(\d+)\.html/i' => 'Single/Single',
	'/^single\/add\.html/i' => 'Single/Save',
	'/^single\/add_(\d+)\.html/i' => 'Single/Save',
	'/^single\/edit_(\d+)\.html/i' => 'Single/Save',
	'/^single\/detail_(\d+)\.html/i' => 'Single/Detail',

	'/^about\.html$/i' => '/Index/About',
	'/^contact\.html$/i' => '/Index/Contact',
));

try {
	$main->run();
} catch (\Lib\Exception\Controller $e) {
	header('HTTP/1.1 404 Not Found');
	header("status: 404 Not Found");
	show404($e);
} catch (\Lib\Exception\Business $e) {
	header('HTTP/1.1 404 Not Found');
	header("status: 404 Not Found");
	show404($e);
} catch (\Exception $e) {
	header('HTTP/1.1 500 Internal Server Error');
	show404($e);
}

function show404($e) {
	$controller = new \Controller\Show404();
	$controller->run(array('msg' => ''));
	throw $e;
}
